<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="88px">
      <el-form-item label="账单日期" prop="billDateInfo">
        <el-date-picker v-model="queryParams.billDateInfo" type="date" placeholder="账单日期" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="子账户类型" prop="itemAccountType">
        <el-input v-model="queryParams.itemAccountType" placeholder="请输入账户类型" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="子账户名称/ID" prop="itemAccountName">
        <el-input v-model="queryParams.itemAccountName" placeholder="请输入子账户名称" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
       <el-form-item label="操作类型" prop="actionType">
        <el-input v-model="queryParams.actionType" placeholder="请输入操作类型" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="总账户名称" prop="mainAccountName">
        <el-input v-model="queryParams.mainAccountName" placeholder="请输入总账户名称" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="商务信息" prop="creatorId">
        <el-input v-model="queryParams.creatorId" placeholder="请输入总账户名称" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="small" @click="handleAdd"
          v-hasPermi="['account:applyRecord:add']">新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="applyRecordList" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="账单ID" align="center" prop="id" />
      <!-- <el-table-column label="操作日期" align="center" prop="dateInfo" /> -->
      <el-table-column label="账单日期" align="center" prop="billDateInfo" />
      <el-table-column label="子账户信息" align="center" prop="itemAccountName">
        <template slot-scope="scope">
            {{scope.row.itemAccountName}} [{{scope.row.itemAccountId}}] <span style="color: blue">[{{scope.row.itemAccountTypeEnumName}}]</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="子账户Id" align="center" prop="itemAccountId" /> -->
      <!-- <el-table-column label="账户类型" align="center" prop="itemAccountTypeEnumName">
        <template slot-scope="scope">
          <span style="color: rgba(226, 17, 17, 0.904)">{{ scope.row.itemAccountTypeEnumName }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="操作类型" align="center" prop="actionTypeEnumName">
        <template slot-scope="scope">
          <span style="color: rgba(226, 17, 17, 0.904)">{{ scope.row.actionTypeEnumName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="总账户" align="center" prop="mainAccountName" >
        <template slot-scope="scope">
                    {{scope.row.mainAccountName}} [{{scope.row.mainAccountId}}]
                </template>
      </el-table-column>

      <!-- <el-table-column label="CZ TK" align="center" prop="mainAccountId" /> -->
      <el-table-column label="入账金额" align="center" prop="billAmount" />
      <el-table-column prop="imgUrl" label="图片">
        <template slot-scope="scope">
          <ImagePreview v-if="!!scope.row.imgUrl" :src="getImage(scope.row)" :width="70" :height="80" />
        </template>
      </el-table-column>
      <el-table-column label="备注信息" align="center" prop="remarkInfo" />
      <el-table-column label="状态 " align="center" prop="statusStr" />
      <el-table-column label="操作员信息" align="center" prop="creatorId" >
       <template slot-scope="scope">
                <p>申请者：{{scope.row.creatorName}}[{{scope.row.creatorId}}]</p>
                <p v-if="scope.row.updatorId != null">入账者：{{scope.row.updatorName}}[{{scope.row.updatorId}}]</p>
        </template>
      </el-table-column>
      <el-table-column label="操作时间" align="center" prop="creatorId" >
       <template slot-scope="scope">
                <p>申请时间：{{ parseTime(scope.row.createTime)}}</p>
                <p v-if="scope.row.updateTime != null">入账时间：{{ parseTime(scope.row.updateTime) }}</p>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-if="scope.row.status == 0" size="small" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['account:applyRecord:edit']">修改</el-button>

          <el-button v-if="scope.row.status == 0" size="small" type="text" icon="el-icon-edit" @click="handleApplyBill(scope.row, 1)"
          v-hasPermi="['account:applyRecord:applyBill']">申请入账</el-button>

          <el-button v-if="scope.row.status == 1" size="small" type="text" icon="el-icon-edit" @click="handleApplyBill(scope.row, 2)"
          v-hasPermi="['account:applyRecord:refuseBill']">驳回</el-button>

          <el-button v-if="scope.row.status == 1" size="small" type="text" icon="el-icon-edit" @click="handleEnterBill(scope.row)"
          v-hasPermi="['account:applyRecord:enterBill']">入账</el-button>

          <el-button v-if="scope.row.status == 0" size="small" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['account:applyRecord:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改账户入账申请对话框 -->
    <el-drawer size="30%" :title="title" :visible.sync="open">
      <div class="demo-drawer-form">
        <div class="demo-drawer-form__content">
          <div class="flex">
            <el-form ref="form" :model="form" :rules="rules" label-width="110px">
             
              <el-form-item label="账单日期" prop="billDateInfo">
                <el-date-picker v-model="form.billDateInfo" type="date" placeholder="账单日期" clearable
                  @keyup.enter.native="handleQuery"  />
              </el-form-item>
   
              <el-form-item label="账户类型" prop="itemAccountType">
                <el-select v-model="form.itemAccountType" placeholder="请选择账户类型" filterable @change="changeSelect($event)">
                  <el-option v-for="item, i in extData.itemAccountTypeMapList" :key="i" :label="item.name" :value="item.value" >
                  </el-option>
                </el-select>
              </el-form-item>

              <el-form-item label="子账户名称" prop="itemAccountId">
                <el-select  v-model="form.itemAccountId" placeholder="请选择子账户名称" filterable><!--修改不可编辑-->
                  <el-option v-for="item, i in actionAndItemAccountData.itemAccountInfoMapList" :key="i" :label="item.name+'('+item.value+')'" :value="item.id">
                  </el-option>
                </el-select>
              </el-form-item>

              <el-form-item label="操作类型" prop="actionType">
                <el-select v-model="form.actionType" placeholder="请选择操作类型">
                  <el-option v-for="(item, i) in actionAndItemAccountData.actionTypeMapList" :key="i" :label="item.name" :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>

               <el-form-item label="入账金额" prop="billAmount">
                <el-input v-model="form.billAmount" placeholder="请输入入账金额" />
              </el-form-item>

              <el-form-item label="总账户信息" prop="mainAccountId">
                <!-- <el-input v-model="form.mainAccountName" placeholder="总账户信息名称" /> -->
                 <el-select v-model="form.mainAccountId" placeholder="请选择总账户信息">
                  <el-option v-for="(item, i) in extData.mainAccountMapList" :key="i" :label="item.name+'('+item.value+')'" :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>

             
              <el-form-item label="备注信息" prop="remarkInfo">
                <el-input v-model="form.remarkInfo" type="textarea" placeholder="请输入内容" />
              </el-form-item>
              <!-- 上传图片---功能 -->
              <el-form-item label="图片信息" prop="imgUrl">
                <ImageUpload v-model="form.imgUrl" :limit="1" bizType="APPLYRECORD" type="cos"/>
              </el-form-item>
     
            </el-form>
          </div>
        </div>
        <div slot="footer" class="dialog-footer demo-drawer-form__footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </div>
    </el-drawer>


<!-- 编辑对话框 -->
    <el-drawer size="30%" :title="title" :visible.sync="openEdit">
      <div class="demo-drawer-form">
        <div class="demo-drawer-form__content">
          <div class="flex">
            <el-form ref="form" :model="form" :rules="rules" label-width="110px">
             
              <el-form-item label="账单日期" prop="billDateInfo">
                <el-input v-model="form.billDateInfo"  />
              </el-form-item>
   
              <el-form-item label="账户类型" prop="itemAccountType">
                <el-input v-model="form.itemAccountTypeEnumName"  />
              </el-form-item>

              <el-form-item label="子账户名称" prop="itemAccountId">
                <el-input v-model="form.itemAccountName"  />
              </el-form-item>

              <el-form-item label="操作类型" prop="actionType">
                <el-input v-model="form.actionTypeEnumName"  />
              </el-form-item>

               <el-form-item label="入账金额" prop="billAmount">
                <el-input v-model="form.billAmount" placeholder="请输入入账金额" />
              </el-form-item>

              <el-form-item label="总账户信息" prop="mainAccountId">
                 <el-select v-model="form.mainAccountId" placeholder="请选择总账户信息">
                  <el-option v-for="(item, i) in extData.mainAccountMapList" :key="i" :label="item.name+'('+item.value+')'" :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>

             
              <el-form-item label="备注信息" prop="remarkInfo">
                <el-input v-model="form.remarkInfo" type="textarea" placeholder="请输入内容" />
              </el-form-item>
              <!-- 上传图片---功能 -->
              <el-form-item label="图片信息" prop="imgUrl">
                <ImageUpload v-model="form.imgUrl" :limit="1" bizType="APPLYRECORD" type="cos"/>
              </el-form-item>
     
            </el-form>
          </div>
        </div>
        <div slot="footer" class="dialog-footer demo-drawer-form__footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </div>
    </el-drawer>


  </div>
</template>

<script>
import { 
  listApplyRecord, 
  getApplyRecord, 
  delApplyRecord, 
  addApplyRecord, 
  updateApplyRecord, 
  baseInfoExtData,
  getActionAndItemAccountData,
  applyBill,
  refuseBill,
  enterBill,
   } from "@/api/account/applyRecord";
import { getToken } from "@/utils/auth";


export default {
  name: "ApplyRecord",
  data() {
    return {
      imgUrl: "",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 账户入账申请表格数据
      applyRecordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openEdit: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        dateInfo: null,
        billDateInfo: null,
        itemAccountName: null,
        itemAccountId: null,
        itemAccountType: null,
        actionType: null,
        mainAccountName: null,
        mainAccountId: null,
        billAmount: null,
        imgUrl: null,
        remarkInfo: null,
        status: null,
        creatorId: null,
        creatorName: null,
        updatorId: null,
        updatorName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        dateInfo: [
          { required: true, message: "您输入的操作日期格式不正确", trigger: "blur" }
        ], 
        mainAccountId: [
          { required: true, message: "您输入的总账户信息不正确", trigger: "blur" }
        ],
        // itemAccountId: [
        //   { required: true, message: "您输入的子账户ID格式不正确", trigger: "blur", pattern: /^\+?[1-9][0-9]*$/ }
        // ],
        itemAccountType: [
          { required: true, message: "您输入的账户类型格式不正确", trigger: "change", pattern: /^.+$/ }
        ],
        actionType: [
          { required: true, message: "您输入的操作类型格式不正确", trigger: "change", pattern: /^.+$/ }
        ],
        billAmount: [
          { required: true, message: "您输入的入账金额格式不正确", trigger: "blur", pattern: /^(?:0\.\d{0,1}[1-9]|(?!0)\d{1,6}(?:\.\d{0,1}[1-9])?)$/ }
        ],
        status: [
          { required: true, message: "您输入的状态格式不正确", trigger: "change" }
        ]
      },
      fileList: [],
      headers: {
        Authorization: "Bearer " + getToken(),
      },
      extData: {},
      actionAndItemAccountData: {}
    };
  },
  created() {
    this.getList();
    this.getExtData();
  },
  methods: {
    changeSelect(value){

      this.form.itemAccountType = value;

      getActionAndItemAccountData(value).then((response) => {
        this.actionAndItemAccountData = response.data;
      });
    },
     getExtData() {
      baseInfoExtData().then((response) => {
        this.extData = response.data;
      });
    },
    handleSuccess(response) {
      // 图片上传成功后的处理逻辑
      console.log(response);
      this.form.imgUrl = response.data;
      this.$modal.msgSuccess(response.msg);
    },
    beforeRemove(file, fileList) {
      return this.$confirm(`确定移除 ${file.name}？`);
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
      console.log('888');
    },

    handleExceed(files, fileList) {
      this.$message.warning(`当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
    },
    beforeRemove(file, fileList) {
      return this.$confirm(`确定移除 ${file.name}？`);
    },
    //获取图片地址
    getImage(row) {
      return row.imgUrl
    },

    /** 查询账户入账申请列表 */
    getList() {
      this.loading = true;
      listApplyRecord(this.queryParams).then(response => {
        this.applyRecordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openEdit= false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        dateInfo: null,
        billDateInfo: null,
        itemAccountName: null,
        itemAccountId: null,
        itemAccountType: null,
        actionType: null,
        mainAccountName: null,
        mainAccountId: null,
        billAmount: null,
        imgUrl: null,
        remarkInfo: null,
        status: null,
        delFlag: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      console.log(this.queryParams);
      if (this.queryParams.billDateInfo) {
        this.queryParams.billDateInfo = this.dayjs(this.queryParams.billDateInfo).format('YYYY-MM-DD');
      }
      if (this.queryParams.dateInfo) {
        this.queryParams.dateInfo = this.dayjs(this.queryParams.dateInfo).format('YYYY-MM-DD');
      }
      // this.queryParams.billDateInfo = getTaskTime(this.queryParams.billDateInfo);
      // this.queryParams.dateInfo = getTaskTime(this.queryParams.dateInfo);
      console.log(this.queryParams)
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {

      this.reset();
      this.open = true;
      this.title = "添加账户入账申请";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids

      getApplyRecord(id).then(response => {
        this.form = response.data;
        this.openEdit = true;
        this.title = "修改总账户信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {

          this.form.createTime = null;
          this.form.updateTime = null;
          if (this.form.id != null) {
            updateApplyRecord(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.openEdit = false;
              this.reset();
              this.getList();
            });
          } else {
            if (this.form.billDateInfo) {
              this.form.billDateInfo = this.dayjs(this.form.billDateInfo).format('YYYY-MM-DD');
            }
            addApplyRecord(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.reset();
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除账户入账申请编号为"' + ids + '"的数据项？').then(function () {
        return delApplyRecord(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 删除按钮操作 */
    handleApplyBill(row, type) {
      const ids = row.id;
      var message = "";
      if(type==1){
        message = '是否确认申请入账编号为"' + ids + '"的数据项？';
      }else{
        message = '是否确认驳回编号为"' + ids + '"的数据项？';
      }

      this.$modal.confirm(message).then(function () {

        if(type==1){
          return applyBill(ids);
        }else{
          return refuseBill(ids);
        } 

      }).then((response) => {

        this.getList();
        this.$modal.msgSuccess("操作成功");

      }).catch(() => { });

    },
    /** 删除按钮操作 */
    handleEnterBill(row) {

      const ids = row.id;
      this.$modal.confirm('是否确认入账编号为"' + ids + '"的数据项？').then(function () {

        return enterBill(ids);

      }).then(() => {

        this.getList();
        this.$modal.msgSuccess("入账成功");


      }).catch(() => { });

    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('account/applyRecord/export', {
        ...this.queryParams
      }, `applyRecord_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
